﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

using IssueTrackerTest.IssueTrackerService;

namespace IssueTrackerTest
{
    /// <summary>
    /// Summary description for IssueWfHistoryTest
    /// </summary>
    [TestClass]
    public class IssueWfHistoryTest
    {
        
  
        [TestMethod]
        public void TestAddIssueWfHistory()
        {
            IssueTrackerService.IssueTrackerServiceClient client
                = new IssueTrackerService.IssueTrackerServiceClient();
            
            IssueTrackerService.IssueWfHistory entity = new IssueTrackerService.IssueWfHistory();

            try
            {
                client.Open();
                List<IssueTrackerService.IssueTask> taskList = client.ListIssueTasks().ToList();

                if (taskList != null)
                {
                    IssueTrackerService.IssueTask task = taskList.First();

                    entity.Id = System.Guid.NewGuid();
                    entity.CreatorId = "testuser";
                    entity.IssueId = task.IssueId;
                    entity.WfActivityId = task.WfActivityId.Value;
                    entity.WfTaskId = task.WfTaskId.Value;

                    IssueTrackerService.IssueWfHistory result = client.CreateIssueWfHistory(entity);

                    Assert.IsNotNull(result.Id);
                }
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.Message);
            }
            finally
            {
                client.Close();
            }
        }

        [TestMethod]
        public void TestListByRef()
        {
            IssueTrackerService.IssueTrackerServiceClient client
                = new IssueTrackerService.IssueTrackerServiceClient();

            client.Open();

            List<IssueTrackerService.Issue> issueList;

            try
            {

                issueList = client.ListIssues().ToList();

                if (issueList != null)
                {
                    List<IssueWfHistory> childList = client.ListIssueWfHistoriesByRef(issueList.First().Id).ToList();

                    Assert.IsNotNull(childList);

                    Assert.IsTrue(childList.Count > 0);
                }

            }
            catch (Exception ex)
            {
                Assert.Fail(ex.Message);
            }
            finally
            {
                client.Close();
            }

        }

        [TestMethod]
        public void TestUpdateIssueWfHistory()
        {
            IssueTrackerService.IssueTrackerServiceClient client
                = new IssueTrackerService.IssueTrackerServiceClient();

            List<IssueTrackerService.IssueWfHistory> list;

            try
            {
                client.Open();
                list = client.ListIssueWfHistories().ToList();

                if (list != null)
                {
                    IssueTrackerService.IssueWfHistory entity = list.First();

                    entity.CreatorId = "newuser";

                    IssueTrackerService.IssueWfHistory result = client.UpdateIssueWfHistory(entity);

                    Assert.AreEqual("newuser", result.CreatorId);
                }
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.Message);
            }
            finally
            {
                client.Close();
            }            
        }

        [TestMethod]
        public void TestDeleteIssueWfHistory()
        {
            IssueTrackerService.IssueTrackerServiceClient client
                = new IssueTrackerService.IssueTrackerServiceClient();

            System.Guid id;
            List<IssueTrackerService.IssueWfHistory> list;

            try
            {
                client.Open();
                list = client.ListIssueWfHistories().ToList();

                if (list != null)
                {
                    IssueTrackerService.IssueWfHistory entity = list.First();

                    id = entity.Id;

                    client.DeleteIssueWfHistory(entity);

                    var checkIssueWfHistory = client.GetIssueWfHistory(id);

                    Assert.IsNull(checkIssueWfHistory);
                }
                else
                {
                    Assert.Fail("IssueWfHistory Table is empty");
                }
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.Message);
            }
            finally
            {
                client.Close();
            }
        }

   
    }
}
